@echo off


set curr=%cd%
pushd %curr%\..\..
set ISERVERDIR=%cd%
popd
set baklog=%curr%\build\ugobak.log

if exist "%curr%\..\jre\bin\java.exe" set "JRE_HOME=%curr%\..\jre"

:init
set /p opr=0(default) to replase, 1 to restore: 
if /I "%opr%" == "" goto check_pkgname
if /I "%opr%" == "0" goto check_pkgname
if /I "%opr%" == "1" goto restore
goto end

:check_pkgname
set /p path=Enter NewUGO URL or LocalPath: 
if /I "%path:~-4%" == ".zip" goto doublecheck
echo Invalid PkgName. Please input a *.zip path or url.
goto check_pkgname

:doublecheck
echo  NewUGO: %path%
echo  TargetPkg: %ISERVERDIR%
set /p has_record=Are you sure to continue? [Y/n]
if /I "%has_record%" == "Y" goto call_ant_replase
if /I "%has_record%" == "" goto call_ant_replase
goto end

:call_ant_replase
echo *** Stating replace... ***
call %curr%/bin/ant -f %curr%/replace-ugo.xml -Dnewugo_url="%path%" -S replacewin
goto end

:restore
echo *** Stating restore... ***
setlocal enabledelayedexpansion 
set n=0
set a[]=""
for /f "delims=" %%i in (%baklog%) do (
    set /a "n+=1" 
    set a[!n!]=%%i
    echo !n!: %%i   
)
set /p record=Select the bak to restore[1-%n%]: 
if /I "%record%" == "" goto restore
if /I "%record%" == "x" goto end
if /I "!a[%record%]!" == "" (
    echo num wrong, record not exist, enter 'x' to exit
    goto restore
)
call %curr%/bin/ant -f %curr%/replace-ugo.xml -Drecord=!a[%record%]! -S restore
goto end

:end
pause